package com.guslegend.controller;


import com.guslegend.enums.BizCodeEnum;
import com.guslegend.exception.BizException;
import com.guslegend.model.AddressDO;
import com.guslegend.request.AddressAddRequest;
import com.guslegend.service.AddressService;
import com.guslegend.util.CommonUtil;
import com.guslegend.util.JsonData;
import com.guslegend.vo.AddressVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 电商-公司收发货地址表 前端控制器
 * </p>
 *
 * @author guslegend
 * @since 2025-05-24
 */
@Api(tags = "收货地址模块")
@RestController
@RequestMapping("/api/address/v1/")
@Slf4j
public class AddressController {

    @Autowired
    private AddressService addressService;

    @ApiOperation("根据id查询地址详细")
    @GetMapping("detail/{address_id}")
    public JsonData detail(
            @ApiParam(value = "地址id",required = true)
            @PathVariable("address_id") String addressId){
        if (CommonUtil.parseLong(addressId) == -1) {
            return JsonData.buildResult(BizCodeEnum.REQUEST_NUMBER_ERROR);
        }
        AddressVO addressVO = addressService.detail(CommonUtil.parseLong(addressId));
        return addressVO == null ? JsonData.buildResult(BizCodeEnum.ADDRESS_NO_EXITS) : JsonData.buildSuccess(addressVO);
    }

    @ApiOperation("添加地址")
    @PostMapping("save")
    private JsonData SaveAddress(
            @ApiParam(value = "地址添加对象")
            @RequestBody AddressAddRequest addressAddRequest){
        int rows=addressService.saveAddress(addressAddRequest);
        return rows==0?JsonData.buildError("添加地址失败"):JsonData.buildSuccess(rows);
    }

    @ApiOperation("删除地址")
    @DeleteMapping("delete/{address_id}")
    public JsonData delete(
            @ApiParam(value = "地址id", required = true)
            @PathVariable("address_id") String addressId
    ){
        if (CommonUtil.parseLong(addressId) == -1) {
            return JsonData.buildResult(BizCodeEnum.REQUEST_NUMBER_ERROR);
        }
        int rows = addressService.delete(CommonUtil.parseLong(addressId));
        return rows == 0 ? JsonData.buildResult(BizCodeEnum.ADDRESS_DEL_FAIL) : JsonData.buildSuccess(rows);
    }

    @ApiOperation("获取用户所有的收货地址")
    @GetMapping("list")
    public JsonData list(){
        return JsonData.buildSuccess(addressService.getAddressList());
    }
}


